# 分镜类
import json
import os
from dataclasses import dataclass, asdict
from typing import List

from global_config import workflows_dir, get_cur_project, workflows_img_dir


@dataclass
class Storyboard:
    start: int = None
    end: int = None
    text: str = None
    img_name: str = None
    local_img: str = None
    ref_img: str = None
    promots: str = None
    prefix: str = None
    suffix: str = None
    aiImgList: list = None
    aiImgNameList: list = None
    # aiImgList_temp: str = None
    # aiImgNameList_temp: str = None
    trigger_id: str = None
    srtbean :[] = None
    select_img:str = None

    def __init__(self, start, end, text, img_name, local_img, ref_img=None, promots=None, prefix=None, suffix=None,
                 aiImgList=[],
                 aiImgNameList=[],
                 trigger_id=None,
                 srtbean:[]=None,
                 select_img: str = None
                 ):
        self.start: int = start
        self.end: int = end
        self.text: str = text
        self.img_name: str = img_name
        self.local_img: str = local_img
        self.ref_img: str = ref_img
        self.promots: str = promots
        self.prefix: str = prefix
        self.suffix: str = suffix
        self.aiImgList: list = aiImgList
        self.aiImgNameList: list = aiImgNameList
        self.trigger_id: str = trigger_id
        if srtbean is None:
            self.srtbean = []
            self.srtbean.append({'start': start, 'end': end, 'text': text})
        else:
            self.srtbean = srtbean
        self.select_img = select_img

    def to_full_prompts(self, global_prompts, global_suffix):
        return f"{(self.prefix + ',') if self.prefix is not None else ''} {self.promots}, {self.suffix + ',' if self.suffix is not None else ','} {global_suffix}, {global_prompts}"

    def to_dict(self):
        return {
            'start': self.start,
            'end': self.end,
            'text': self.text,
            'img_name': self.img_name,
            'local_img': self.local_img,
            'ref_img': self.ref_img,
            'promots': self.promots,
            'prefix': self.prefix,
            'suffix': self.suffix,
            'aiImgList': self.aiImgList,
        }

    def to_json(self):
        """Converts the Storyboard instance to a JSON formatted string."""
        return json.dumps(asdict(self))

    # midjourney 返回的是4合1 图片
    # 本地储存的是分割以后的图片
    def get_download_split_images(self):
        image_temp_dir = f'{workflows_dir}{get_cur_project()}\\{workflows_img_dir}\\'
        if not os.path.exists(image_temp_dir):
            os.mkdir(image_temp_dir)

        split_images = []
        for image in self.aiImgNameList:
            for i in range(1, 5):
                split_images.append(f"{image_temp_dir}0{i}_{image}")

        return split_images


@dataclass
class ProjectBean:
    storylist: list[Storyboard]
    global_suffix: str = None

    def __init__(self, storylist, global_suffix):
        self.storylist = storylist
        self.global_suffix: str = global_suffix

    def to_dict(self):
        return {
            'storylist': self.storylist,
            'global_suffix': self.global_suffix,
        }

    def to_json(self):
        """Converts the Storyboard instance to a JSON formatted string."""
        return json.dumps(asdict(self))
